home *** CD-ROM | disk | FTP | other *** search
/ Commodore Free 10 / Commodore_Free_Issue_10_2007_Commodore_Computer_Club.d64 / t.slang tut < prev    next >
Text File  |  2023-02-26  |  11KB  |  395 lines

  1. u        SLANG TUTORIAL PART 1
  2.  
  3.           A simple tutorial:
  4.   Transitioning to Slang from BASIC
  5. ======================================
  6.  
  7.  
  8. If you're familiar with higher-level
  9. languages you can probably skip this
  10. part. Some people however may be
  11. familiar primarily with programming in
  12. Commodore BASIC, and that's who this
  13. section is targeted at. This section
  14. introduces some of the higher-level
  15. language features in Slang you'll need
  16. to be aware of, in comparison with
  17. BASIC.
  18.  
  19. This is also a little tutorial on
  20. using Slang, so there are lots of
  21. examples below to try out. Once you've
  22. gone through them you should be well
  23. on your way to using Slang like a pro.
  24.  
  25. Note: If you find this tutorial useful
  26. -- or if you find it totally confusing
  27.  
  28. or if you find it anything at all --
  29. please write me or post a note to the
  30. forum, and let me know. I'd love to
  31. hear from you. Heck, right now I'd
  32. love to hear from anybody! But
  33. feedback is very important, and it
  34. helps me to know what works and what
  35. doesn't.
  36.  
  37. Interpreted vs. Compiled
  38. BASIC is an interpreted language,
  39. whereas Slang is a compiled language.
  40. In BASIC, you write the code, then RUN
  41. it, then errors are caught as they are
  42. encountered (Syntax Errors, Overflow
  43. Errors, etc.). You can break a program
  44. while it's running, have a look at
  45. some of the variables, change things
  46. around, then CONTinue running the
  47. program.
  48.  
  49. In a compiled language like Slang you
  50. write the code, then compile it into
  51. machine language, then run that
  52. machine language program. You don't
  53. actually run the code you've written
  54. -- the compiler converts the code
  55. you've written into machine language,
  56. which you then run.
  57.  
  58. So there are actually two parts to the
  59. program, that you can for example save
  60. to disk: the Dsource codeD,which is
  61. the part you write, and the Dobject
  62. codeD which is what the compiler
  63. produces. The object code is what
  64. other people will run whenthey run
  65. your program. In Slang, source code
  66. files end in '.s', as in
  67. "spritedemo.e.s", and object files end
  68. in '.o', as in "spritedemo.e.o"
  69.  
  70. As to errors, errors such as Syntax
  71. Errors are caught during the
  72. DcompileDphase, before ever running
  73. the program. Other types of errors
  74. (called runtime errors) are never
  75. caught at all -- in general if there
  76. are errors in your program it will
  77. keep on running, or may lock up the
  78. machine entirely! But Slang is
  79. designed to handle this, so that you
  80. won't lose your program.
  81.  
  82. Tutorial lesson #1:
  83. So here is the first thing to try:
  84. we're going to write a program that
  85. will crash, so you can lose your fear
  86. right away of causing some huge
  87. problem. To start up slang, load and
  88. run the main program (slangb1.9.o or
  89. whatever it might be). Once you've got
  90. the editor running, type in the
  91. following program:
  92.  
  93. sprint "here comes a crash! "
  94.  
  95. donebrk
  96.  
  97. Then press F1 to compile the program.
  98. If there are no errors, you will get a
  99. "compile successful" message. Now
  100. press F4 to run the program - crash,
  101. right? Go ahead and reset the machine
  102. (don't power it down; just press the
  103. reset button). Then type
  104.  
  105. sys 54016
  106.  
  107. and you should pop back into Slang.
  108. Slang stores itself up in SuperRAM, so
  109. you don't need to worry about losing
  110. your program because of a crash, and
  111. if you want to try something just go
  112. ahead and experiment!
  113.  
  114. Variables
  115. In BASIC variables
  116. are created on the fly -- you just say
  117. "10 a=1" and off you go. In languages
  118. like Slang, you have to declare your
  119. variables before using them. Let's
  120. take a dumb BASIC program, and convert
  121. it to Slang:
  122.  
  123. 10 FOR B=1 TO 3:PRINT "BLAH ";:NEXT
  124. 20 PRINT CHR$(13)+"PRESS ANY KEY" 30
  125. GET A$:IF A$="" THEN 30 40 END
  126.  
  127. Obviously lines 20-40 are not needed,
  128. but I put them in for a reason.
  129. Here's a Slang version:
  130.  
  131. byte b for b=1:3 sprint "blah " next
  132. sprint !13"press any key..." waitchar
  133. done
  134.  
  135. Tutorial lesson #2:
  136. Go ahead and type this program into
  137. the Slang editor, press F1 to compile,
  138. and press F4 to run. Easy! The first
  139. line of this program DdeclaresD the
  140. variable. Part of the reason you have
  141. to do this is that there are different
  142. Dvariable typesD available. In BASIC,
  143. you can have floating-point variables,
  144. integers, and strings, and the name
  145. itself tells basic what type of
  146. variable it is:
  147.  
  148. a=10 ;numeric (floating point)
  149. variable a%=10 ;numeric (integer)
  150. variable a$="10" ;string variable
  151.  
  152. As you probably know, the only reason
  153. to use integer variables is with
  154. arrays, to save memory, because an
  155. integer array takes less memory than a
  156. float. Otherwise, a plain integer
  157. variable takes exactly as much space
  158. as a regular variable, and is actually
  159. slower for calculations because all
  160. calculations in BASIC are
  161. floating-point calculations.
  162.  
  163. In Slang, this is not the case. The
  164. variable type not only changes how
  165. much space is used but also how the
  166. variable is manipulated. In Slang,
  167. adding integers is much faster than
  168. adding floats together, and the
  169. integers take less space. For example,
  170. if you change the line
  171.  
  172. byte b
  173.  
  174. in the program above to
  175.  
  176. float b
  177.  
  178. you'll find that the program becomes
  179. larger, and it's also much slower
  180. (although you won't notice that in a
  181. simple program like this)
  182.  
  183. Tutorial lesson #3:
  184. Change b from a byte to a float,
  185. compile, and see what happens. The
  186. different variable types available in
  187. Slang are:
  188.  
  189. byte - 1 byte, signed numbers in range
  190. -128..127 ubyte - 1 byte, unsigned,
  191. range = 0..255 int - 2 bytes, signed,
  192. range = -32768..32767 uint - 2 bytes,
  193. unsigned, range = 0..65535 float - 5
  194. bytes, signed, range = well, the usual
  195. BASIC range
  196.  
  197. In general, the smaller variables are
  198. also faster, so you'll often choose
  199. the simplest variable that meets your
  200. needs. Just a few other things are
  201. worth noting. Unlike BASIC, variables
  202. can also have really long names, like:
  203.  
  204. uint ThisIsAVeryLongVariableName
  205.  
  206. b = b+ThisIsAVeryLongVariableName
  207.  
  208. Note also that variables can mix upper
  209. and lower case (the compiler is case-
  210. insensitive), and can mix variables of
  211. different types (you can add a byte to
  212. an int, for example). And once a
  213. variable is declared, that's it -- you
  214. can't re define a variable as a
  215. different type.
  216.  
  217. One other thing to notice is that
  218. there is only one statement per line.
  219. Unlike BASIC, you cannot put multiple
  220. commands on the same line.
  221.  
  222. Loops and such
  223. As is apparent in the above program,
  224. the for-loop in Slang is pretty
  225. similar to the BASIC for-loop. You can
  226. also put a "step" in there:
  227.  
  228. for b=1:10 step 3
  229.  
  230. There are two other loop structures
  231. available in Slang (and many other
  232. lanauges): while-loops, and
  233. repeat-loops. Here's an example:
  234.  
  235. b=1 while b<4 sprint "blah " b=b+1
  236. endwhile
  237.  
  238. This program does exactly the same
  239. thing that the for-loop does in the
  240. original program: while the expression
  241. b<4 is true, it performs whatever code
  242. is in-between the "while" and
  243. "endwhile" statements. The repeat
  244. statement is very similar:
  245.  
  246. b=1 repeat sprint "blah" b=b+1 until
  247. b>3
  248.  
  249. A repeat-statement always executes at
  250. least once, because the expression
  251. check is at the end of the loop. The
  252. way to think about all these things
  253. is pretty simple:
  254.  
  255. while [expression] While [expression]
  256. is true [code] <-------------Execute
  257. this block of code endwhile between
  258. the while and endwhile statements One
  259. thing that Slang does not have is a
  260. GOTO statement. You can actually do
  261. gotos using assembly language, but it
  262. turns out that you can handle all the
  263. things you might use GOTO for using
  264. for/repeat/while-loops and the
  265. if-endif structure discussed down
  266. below, and your programs will be
  267. easier to debug.
  268.  
  269. Tutorial lesson #4:
  270. Replace the for-loop in the example
  271. code with a) a while-loop, and b) a
  272. repeat- until loop, but make it print
  273. out the text five times instead of
  274. three. Compile and run each case to
  275. verify that it works.
  276.  
  277. Ending a program You'll notice that
  278. the program ends with the line "done".
  279. In BASIC, putting an "END" in is
  280. optional. In Slang, it is required. It
  281. tells the C64 how and when to exit the
  282. program. If you'd like to see what
  283. happens when you leave it out, go
  284. ahead and try it! Just remember that
  285. "sys 54016" will restart Slang after
  286. you reset the computer.
  287.  
  288. We can now explain the last few lines
  289. as well. The command "waitchar" simply
  290. waits for a key to be pressed. Without
  291. this line, the program would
  292. immediately end and return you back to
  293. Slang. The "waitchar" would not be
  294. necessary if this program were being
  295. called from, say, BASIC -- in
  296. thatcase, you would probably want
  297. control to return to BASIC
  298. immediately.
  299.  
  300. But when running programs directly
  301. from the Slang editor, you'll usually
  302. wantto put a line like this at the
  303. end of the program.
  304.  
  305. Tutorial lesson #5:
  306. As a test, go ahead and comment out
  307. that line (place a ";" before the
  308. waitchar re-compile, and see what
  309. happens when you run the program!
  310.  
  311. Print The little program above used
  312. the "sprint" command. There are
  313. actually two print commands in Slang:
  314. print, and sprint.
  315.  
  316. Sprint -- Simple Print, or String
  317. Print -- is a simple print command
  318. that only prints strings (not
  319. numbers). As we shall see shortly, the
  320. regular print command contains certain
  321. things in a DlibraryD, which requires
  322. an extra step, and will make your
  323. programs larger. For the moment though
  324. we can focus on sprint.
  325.  
  326. Printing a string in Slang works much
  327. the same in BASIC, with just a few
  328. little twists. Unlike in BASIC, you
  329. cannot embed special characters inside
  330. the quotes, like
  331.  
  332. 10 PRINT "[ctrl-2][shft-clr]nice
  333. white text on a clear screen."
  334.  
  335. Instead, in Slang, you have to use the
  336. character codes directly; in BASIC,
  337. you could also do the above as
  338.  
  339. 10 PRINT CHR$(5)+CHR$(147)+"nice
  340. white text on a clear screen."
  341.  
  342. In Slang, you simply use a ! instead
  343. of chr$, so this would look like
  344.  
  345. sprint !5!147"nice white text on a
  346. clear screen."
  347.  
  348. The above command has one key
  349. difference from the BASIC version,
  350. however :in BASIC, PRINT normally
  351. prints a chr$(13) at the end of the
  352. line, whereassprint will not. It turns
  353. out that there's actually two more
  354. print commands in Slang, though:
  355. println and sprintln. These work
  356. exactly the same as print and sprint,
  357. but print an extra [RETURN] character
  358. at the end.
  359.  
  360. Tutorial lesson #6:
  361. Go ahead and try replacing "sprint"
  362. with "sprintln" in the example
  363. program,and see what happens.
  364.  
  365. Tutorial lesson #7:
  366. Modify the example program to print
  367. out the text in cyan. How about light
  368. green?
  369.  
  370. There's one more trick to print and
  371. sprint: you can specify DwhereD on
  372. the screen to print:
  373.  
  374. sprint(0,20) "some text"
  375.  
  376. will print the text at row=0,
  377. column=20.
  378.  
  379. Tutorial lesson #8:
  380. Modify the program to print the text
  381. to the middle of the screen. In
  382. contrast to sprint, print can print
  383. numbers and strings, and you can stick
  384. them all together on a single line.
  385. The catch is that manyof the routines
  386. needed to do this are in a DlibraryD.
  387. This library is a file on the disk,
  388. containing special routines. In
  389. general, libraries can be source code,
  390. object code, or a special kind of file
  391. (a relocatable file) for use by the
  392. linker, which I won't talk about here.
  393.  
  394. CONTINUED IN PART 2
  395.  
  396.